<template>
	<div id="areaCodeList" class="same-change-oldstyle">
		<div class="same-head-box">
			<div class="saem-seat-box">
				<div class="saem-seat-left">
					<span>当前位置：</span>
					<el-breadcrumb separator-class="el-icon-arrow-right">
						<el-breadcrumb-item :to="{ path: '/communityList' }">园区管理</el-breadcrumb-item>
						<el-breadcrumb-item>区域码管理</el-breadcrumb-item>
					</el-breadcrumb>
				</div>
				<div class="saem-seat-right">
					<button class="same-search-btns same-search-export" @click="RefreshFun">刷新</button>
					<!-- <button class="same-search-btns" @click="returnBack">返回</button> -->
				</div>
			</div>
			<div class="same-search-box">
				<div>
					<div class="same-search-left">
						<div class="same-left-one">
							<span class="same-search-span same-search-fisrt">添加时间:</span>
							<el-date-picker style="width:140px" v-model="date1" type="date" size="medium" value-format="yyyy-MM-dd"
								placeholder="开始时间"></el-date-picker>
							<div class="same-interval">至</div>
							<el-date-picker style="width:140px" v-model="date2" type="date" size="medium" value-format="yyyy-MM-dd"
								placeholder="结束时间"></el-date-picker>
						</div>
						<div class="same-left-one">
							<span class="same-search-span">区域码:</span>
							<el-input size="medium" v-model="area_code" placeholder="请输入区域码" style="width: 160px;"></el-input>
						</div>
						<div class="same-left-one" v-if="!moveShow">
							<div class="same-left-zdshwo" :hidden="!moveBtn" @click="openMore">更多查询条件<i
									:class="[moveShow?'el-icon-arrow-up':'el-icon-arrow-down']"></i></div>
							<el-button type="primary" size="medium" @click="searchFun()">搜索</el-button>
							<el-button type="info" size="medium" @click="reset()">重置</el-button>
						</div>
					</div>
					<div class="same-search-right"></div>
				</div>
				<!-- 更多搜索条件 -->
				<div>
					<div class="same-search-left new-sear-height" ref="leftHeight">
						<div class="same-left-one">
							<span class="same-search-span">状态:</span>
							<el-select size="medium" v-model="is_frozen" placeholder="请选择" style="width: 140px;">
								<el-option v-for="item in stateList" :key="item.stateId" :label="item.stateName" :value="item.stateId">
								</el-option>
							</el-select>
						</div>
						<div class="same-left-one" :class="[moveShow?'':'new-sear-opacity']">
							<div class="same-left-zdshwo" :hidden="!moveBtn" @click="openMore">收起更多查询条件<i
									:class="[moveShow?'el-icon-arrow-up':'el-icon-arrow-down']"></i></div>
							<el-button type="primary" size="medium" @click="searchFun()">搜索</el-button>
							<el-button type="info" size="medium" @click="reset()">重置</el-button>
						</div>
					</div>
					<div class="same-search-right"></div>
				</div>
			</div>
		</div>

		<div class="same-table-box">
			<div class="tabel_header">
				<h4 class="same-title-box">区域码管理
					<div class="same-search-total"></div>
				</h4>
				<div class="same-left-one"> 
					<el-button type="primary" size="mini" @click="addAreaCode()" icon="el-icon-circle-plus">添加区域码</el-button>
				</div>
			</div>

			<div class="common-table-outer">
				<el-table class="common-table" v-loading="loading" header-cell-class-name="common-table-header-cell"
					:data="tableData" stripe>
					<el-table-column header-align="center"  width="70" align="center" label="序号">
						<template slot-scope="scope">
							{{scope.$index+1+(pageNo-1)*pageSize}}
						</template>
					</el-table-column>
					<el-table-column align="center" prop="name" label="名称">
					</el-table-column>
					<el-table-column align="center" prop="area_code" label="区域码号">
					</el-table-column>
					<el-table-column align="center" label="当前状态">
						<template slot-scope="scope">
							<el-tag size="mini" type="success" v-if="scope.row.is_frozen == 1">正常</el-tag>
							<el-tag size="mini" type="warning" v-else-if="scope.row.is_frozen == -1">冻结</el-tag>
							<span v-else>——</span>
						</template>
					</el-table-column>
					<el-table-column align="center" prop="add_time" label="创建时间">
					</el-table-column>
					<el-table-column align="center" label="操作">
						<template slot-scope="scope">
							<el-link  class="custom-link" @click="frCardMother(scope.row.id,scope.row.is_frozen)"
								v-if="scope.row.is_frozen == 1">冻结</el-link>
							<el-link class="custom-link"  @click="frCardMother(scope.row.id,scope.row.is_frozen)"
								v-else-if="scope.row.is_frozen == -1">解冻</el-link>
						</template>
					</el-table-column>
					<template slot="empty" v-if="isLoad">
						<div class="new-no-data-show">
							<img src="../../assets/images/no-data.png" alt="">
							<div>暂无相关数据</div>
						</div>
					</template>
				</el-table>
				<div class="same-page-inShow" v-if="tableData.length > 0 && isLoad">
					<el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
						:current-page.sync="pageNo" :page-sizes="pageSizesList" :page-size="pageSize"
						layout="total, prev, pager, next, jumper, sizes" :total="totalDataNumber"></el-pagination>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	export default {
		name: 'areaCodeList',
		data: function () {
			return {
				date1: '',
				date2: '',
				area_code: '', //区域码
				stateList: [
					{
						stateId: '',
						stateName: '全部'
					},
					{
						stateId: 1,
						stateName: '正常'
					},
					{
						stateId: -1,
						stateName: '冻结'
					}
				],
				is_frozen: '', //选择的状态，1正常-1冻结
				pageNo: 1,
				pageSize: 15,
				pageSizesList: [15, 30, 40, 50],
				tableData: [], // 返回的结果集合
				totalDataNumber: 0, // 数据的总数,-----
				loading: false, //加载动画
				isLoad: false,
				userAgent: '',

				moveBtn: false,//是否暂时更多查询条件按钮按钮 搜索条件控制
				moveShow: false,//是否开启动画
				scrollHeight: '',//需要监听的高度
			};
		},
		// 初始化执行
		created: function () {
			this.$emit('gitHidden', true);//是否隐藏底部版权 true 隐藏 
			var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
			this.userAgent = userAgent;
			var that = this;
			var page = that.pageNo; // 当前页
			var pageSize = that.pageSize; // 每页条数
			that.areaCodeList(page, pageSize); // 获取区域码列表数据
		},
		inject: [
			'reload'
		],
		methods: {
			RefreshFun() {
				// 调用reload方法，刷新整个页面
				this.reload()
			},
			//返回
			returnBack() {
				this.$router.back(-1);
			},
			areaCodeList(page, pageSize) {
				var that = this;
				that.loading = true; //打开loading动画
				that.isLoad = false; //是否加载
				// Ajax请求
				that.$post({
					url: 'Partner/CardMother/cardMotherList',
					data: {
						area_code: that.area_code,
						start_time: that.date1 ? that.date1 : '',
						end_time: that.date2 ? that.date2 : '',
						is_frozen: that.is_frozen,
						pageSize: that.pageSize,
						page: that.pageNo
					},
					success: data => {
						that.loading = false; //打开loading动画
						that.isLoad = true; //是否加载
						that.tableData = [];
						//console.log(data);
						if (data.code == 1000) {
							//that.$message.success(data.message);
							that.tableData = data.data.data;
							that.totalDataNumber = data.data.page.total_number;
							that.pageNo = data.data.page.current_page;
						} else {
							// that.$message.error(data.message);
						}
					}
				});
			},
			// 每页
			handleSizeChange(val) {
				var that = this;
				that.pageSize = val; // 当前每页显示条数
				that.pageNo = 1; // 当前页
				that.areaCodeList(that.pageNo, val); // 获取设备列表数据
			},
			// 分页
			handleCurrentChange(val) {
				var that = this;
				var pageSize = that.pageSize; // 每页条数
				that.pageNo = val;
				that.areaCodeList(val, pageSize); // 获取设备列表数据
			},

			management: function (id) {
				this.$router.push({ path: '/management', query: { id: id } });
			},
			//解冻冻结
			frCardMother(id, is_frozen) {
				var that = this;
				var text = is_frozen == 1 ? '是否冻结?' : '是否解冻?'
				this.$confirm(text, '提示', { type: 'warning', center: true }).then(() => {
					// Ajax请求
					that.$post({
						url: 'Partner/CardMother/frCardMother',
						data: {
							code_id: id
						},
						success: data => {
							if (data.code == 1000) {
								//成功后更新列表，且在当前页和当前每页条数
								that.areaCodeList(that.pageNo, that.pageSize);
							} else {
								that.$message.error(data.message);
							}
						}
					});
				}).catch(() => {

				});

			},
			//添加区域码
			addAreaCode() {
				this.$router.push({ path: '/areaCodeAdd' });
			},

			//重置
			reset() {
				this.pageNo = 1;
				this.date1 = '';
				this.date2 = '';
				this.is_frozen = '';
				this.area_code = '';
				this.areaCodeList(this.pageNo, this.pageSize);
			},
			//搜索园区
			searchFun() {
				var that = this;
				var page = that.pageNo; // 当前页
				var pageSize = that.pageSize; // 每页条数
				that.areaCodeList(page, pageSize); // 获取园区列表数据
			},

			//此处获取高度必须在dom元素创建完成之后才能获取
			gitHeight() {
				//  let clientHeight=this.$refs.leftHeight.clientHeight;
				let scrollHeight = this.$refs.leftHeight.scrollHeight;
				this.scrollHeight = scrollHeight;
				scrollHeight <= 30 ? this.moveBtn = false : this.moveBtn = true;

			},
			openMore() {
				this.moveShow = !this.moveShow;
				this.$refs.leftHeight.style.height = this.moveShow ? this.scrollHeight + 'px' : 0 + 'px';
			},
		},
		mounted() {
			this.gitHeight();
			//监听高度变化 
			var that = this;
			window.addEventListener("resize", function () {
				let scrollHeight = that.$refs.leftHeight && that.$refs.leftHeight.scrollHeight ? that.$refs.leftHeight.scrollHeight : that.scrollHeight;
				that.scrollHeight = scrollHeight;
			});
		},
		watch: {
			// 如果 `scrollHeight` 发生改变，这个函数就会运行 此处是为了防止用户缩小屏幕导致页面显示不全
			scrollHeight: function (val) {
				this.$refs.leftHeight.style.height = this.moveShow ? this.scrollHeight + 'px' : 0 + 'px';
			}
		},
		activated() {
			if (this.$route.meta.isUpdate) {
				this.tableData = [];
				this.areaCodeList(this.pageNo, this.pageSize);
				this.$route.meta.isUpdate = false;
			}
		},

    beforeRouteEnter (to, from, next) {
      let updates = ["/areaCodeAdd"]
			if (updates.includes(from.path)) {
				to.meta.isUpdate = true;
			}
      next();
		},
		beforeRouteLeave(to, from, next) {
			let updates = ["/areaCodeAdd"];
			if (!updates.includes(to.path)) {
				from.meta.keepAlive = false; 
			} else {
				from.meta.keepAlive = true;
			}
			next();
		}
	};
</script>

<style>
	#areaCodeList {
		width: 100%;
		height: 100%;
		background: #ffffff;
	}
</style>